﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
namespace Nivdal
{
    public partial class ForgotThePassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.ForgotThePassword_PageTitle;
            if (HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/TeamHomePage.aspx");

            CultureInfo Cult = ResolveCulture();
            if ((Cult != null) && (Cult.TextInfo.IsRightToLeft))
            {   
                CorrectDirPanel.Direction = ContentDirection.RightToLeft;
                DirStylesheetLiteral.Text = "<link type=\"text/css\" rel=\"stylesheet\" href=\"stylesheetRTL.css\" />";
                FacebookPanel.Direction = ContentDirection.RightToLeft;
                PostCodePanel.Direction = ContentDirection.RightToLeft;
                (LoginView1.FindControl("LoginPanel") as Panel).Direction = ContentDirection.RightToLeft;
            }
            else
            {
                DirStylesheetLiteral.Text = "<link type=\"text/css\" rel=\"stylesheet\" href=\"stylesheetLTR.css\" />";
            }

            if (!IsPostBack)
            {
                if (Request.QueryString["Code"] != null)
                {
                    string Code = Request.QueryString["Code"];

                    GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                    string UserName = GND.ReadNivdalStringData("Code", "Nivdal_GetUserNameByPasswordResetCode", Code);
                    if (UserName.Length > 0)
                    {
                        string Email = GND.ReadNivdalStringData("UserName", "Nivdal_GetEmailByUserName", UserName);
                        
                        PasswordRecovery1.UserName = UserName;

                        (PasswordRecovery1.UserNameTemplateContainer.FindControl("EmailLabel") as Label).Text = Email;

                    }
                    else
                    {
                        PasswordRecovery1.Visible = false;
                        InvalidCodePanel.Visible = true;
                    }
                }
                else
                {
                    PasswordRecovery1.Visible = false;
                    InvalidCodePanel.Visible = true;
                }
            }

        }
        

        public static CultureInfo ResolveCulture()
        {
            string[] languages = HttpContext.Current.Request.UserLanguages;
            if (languages == null || languages.Length == 0)
                return null;
            try
            {
                string language = languages[0].ToLowerInvariant().Trim();
                return System.Globalization.CultureInfo.CreateSpecificCulture(language);
            }
            catch (ArgumentException)
            {
                return null;
            }
        }

        

        protected void SendingEmail(object sender, MailMessageEventArgs e)
        { 
            System.Net.Mail.SmtpClient smtpSender = new System.Net.Mail.SmtpClient(); 
            //smtpSender.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            //smtpSender.Credentials = new System.Net.NetworkCredential("nivdal@gmail.com", "pasha2@2");
            //smtpSender.EnableSsl = true;
            //smtpSender.UseDefaultCredentials = false;
            smtpSender.Send(e.Message); 
            e.Cancel = true; 
        } 
    }
}